<template>
  <div class="app-container">
    <FilterSearch :value="queryParams" :config="configData" layout="inline"></FilterSearch>
    <main>
      <div class="table">
        <a-table :scroll="{ x: 1400 }" bordered :dataSource="tableData.dataSource" :columns="tableData.columns"
          :pagination="false" :loading="tableData.loading">
          <template #bodyCell="{ column, index, record }">
            <template v-if="column.key === 'action'">
              <a-button type="link" @click="handleChange(record, 'edit')">
                编辑
              </a-button>
              <a-button type="link" @click="handleChange(record, 'record')">
                操作记录
              </a-button>
            </template>
            <template v-if="column.key === 'index'">
              <span>
                {{ (index + 1) }}
              </span>
            </template>
            <template v-if="column.key === 'state'">
              <template v-for="item in typeOptions.options1">
                <span v-if="record.state == item.codeValue" color="#f50">{{ item.codeName }}</span>
              </template>
            </template>
            <template v-if="column.key === 'serviceType'">
              <template v-for="item in typeOptions.options3">
                <span v-if="record.serviceType == item.codeValue" color="#f50">{{ item.codeName }}</span>
              </template>
            </template>
            <template v-if="column.key === 'serviceMode'">
              <template v-for="item in typeOptions.options4">
                <span v-if="record.serviceMode == item.codeValue" color="#f50">{{ item.codeName }}</span>
              </template>
            </template>
          </template>
        </a-table>
      </div>
      <div class="page">
        <a-pagination v-model:current="queryParams.pageNumber" :show-total="total => ` 总条数:${total}条`"
          v-model:pageSize="queryParams.pageSize" :total="tableData.total" @change="currentChange" />
      </div>
    </main>

    <a-modal v-model:open="modalOpen.open1" style="width:75%;" title="编辑" :maskClosable="false" :closable="false">
      <div class="modal-body">
        <a-form :model="formState" disabled :rules="rules" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }"
          autocomplete="off" ref="formStateEdit">
          <a-row>
            <a-col :span="8">
              <a-form-item label="合同号" name="contractNum">
                <a-input v-model:value="formState.contractNum" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="身份证号" name="idCard">
                <a-input v-model:value="formState.idCard" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="业务模式" name="serviceMode">
                <a-select allowClear v-model:value="formState.serviceMode">
                  <a-select-option v-for="(item, index) in typeOptions.options4" :value="item.codeValue"
                    :key="item.codeValue">
                    {{ item.codeName }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="承租人" name="lesseeName">
                <a-input v-model:value="formState.lesseeName" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="业务类型" name="serviceType">
                <a-select allowClear v-model:value="formState.serviceType">
                  <a-select-option v-for="(item, index) in typeOptions.options3" :value="item.codeValue"
                    :key="item.codeValue">
                    {{ item.codeName }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="档案存储地" name="fileLocation">
                <a-input v-model:value="formState.fileLocation" :disabled="archivesNumDisable" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="车架号" name="carNum">
                <a-input v-model:value="formState.carNum" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="渠道描述" name="channelDesc">
                <a-input v-model:value="formState.channelDesc" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="入库时间" name="warehouseTime">
                <a-input v-model:value="formState.warehouseTime" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="车牌号" name="licensePlateNum">
                <a-input v-model:value="formState.licensePlateNum" :disabled="archivesNumDisable" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="二网经销商" name="secondDealer">
                <a-input v-model:value="formState.secondDealer" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="移库时间" name="moveLibTime">
                <a-input v-model:value="formState.moveLibTime" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="档案状态" name="state">
                <a-select allowClear v-model:value="formState.state" :disabled="archivesNumDisable">
                  <a-select-option v-for="(item, index) in typeOptions.options1" :value="item.codeValue"
                    :key="item.codeValue">
                    {{ item.codeName }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="出库来源" name="outboundSource">
                <a-select allowClear v-model:value="formState.outboundSource" :disabled="archivesNumDisable">
                  <a-select-option v-for="(item, index) in typeOptions.options2" :value="item.codeValue"
                    :key="item.codeValue">
                    {{ item.codeName }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item :label-col="{ span: 2 }" :wrapper-col="{ span: 22 }" label="备注" name="remark">
                <a-textarea :disabled="archivesNumDisable" v-model:value="formState.remark"
                  :auto-size="{ minRows: 3, maxRows: 3 }" />
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <template #footer>
        <div class="modal-btns">
          <div class="modal-btn">
            <a-button type="primary" @click="handleChange({}, 'editOk')">确认</a-button>
          </div>
          <div class="modal-btn">
            <a-button @click="cancel('edit')">关闭</a-button>
          </div>
        </div>
      </template>
    </a-modal>

    <a-modal v-model:open="modalOpen.open2" style="width:50%;" title="档案操作记录" :maskClosable="false" :closable="false">
      <div class="modal-body">
        <div class="table">
          <a-table bordered :dataSource="tableData.modalDataSource" :columns="tableData.modalColumns" :pagination="{ total: tableData.modalDataSource.length, showTotal: total => ` 总条数:${total}条` }">
            <template #bodyCell="{ column, record }">
              <template v-if="column.key === 'createdBy'">
                <span>
                  {{ record.createdByName }}
                </span>
              </template>
              <template v-if="column.key === 'eventNode'">
                <template v-for="item in typeOptions.options5">
                  <span v-if="record.eventNode == item.codeValue" style="text-align:center;" color="#f50">{{
                    item.codeName }}</span>
                </template>
              </template>
            </template>
          </a-table>
        </div>
      </div>
      <template #footer>
        <div class="modal-btns">
          <div class="modal-btn">
            <a-button @click="cancel('record')">关闭</a-button>
          </div>
        </div>
      </template>
    </a-modal>

    <a-modal v-model:open="modalOpen.open3" :title="uploadErr.title" :maskClosable="false" :closable="false">
      <div class="modal-body">
        <div class="form">
          {{ uploadErr.tips }}
        </div>
      </div>
      <template #footer>
        <div class="modal-btns">
          <div class="modal-btn" v-if="uploadErr.data.length > 0">
            <a-button type="primary" @click="handleChange({}, 'downloadErr')">下载</a-button>
          </div>
          <div class="modal-btn">
            <a-button @click="cancel('downloadErr')">关闭</a-button>
          </div>
        </div>
      </template>
    </a-modal>

    <a-modal v-model:open="modalOpen.open4" title="设置归还时效" style="width:350px;" :maskClosable="false" :closable="false">
      <div class="modal-body">
        <div class="form">
          <a-row>
            <a-col :span="24">
              <a-form-item label="归还时效" name="date">
                <a-input v-model:value="formState.date" @input="formState.date = formState.date.replace(/[^0-9]/g, '')">
                  <template #addonAfter>
                    天
                  </template>
                </a-input>
              </a-form-item>
            </a-col>
          </a-row>
        </div>
      </div>
      <template #footer>
        <div class="modal-btns">
          <div class="modal-btn">
            <a-button @click="handleChange({}, 'time')" type="primary">确认</a-button>
          </div>
          <div class="modal-btn">
            <a-button @click="cancel('time')">关闭</a-button>
          </div>
        </div>
      </template>
    </a-modal>

    <a-modal v-model:open="modalOpen.open5" title="下载模板" :maskClosable="false" :closable="false">
      <div class="modal-body">
        <div class="modal-btns">
          <div class="modal-btn">
            <a-button type="primary" @click="handleChange({}, 'downloadUpdate')">
              下载批量修改模板
            </a-button>
          </div>
          <div class="modal-btn">
            <a-button type="primary" @click="handleChange({}, 'downloadDeposit')">
              下载批量转库模板
            </a-button>
          </div>
        </div>
      </div>
      <template #footer>
        <div class="modal-btns">
          <div class="modal-btn">
            <a-button @click="cancel('downloadTemplate')">关闭</a-button>
          </div>
        </div>
      </template>
    </a-modal>
    <a-modal v-model:open="modalOpen.open6" style="width:75%;" title="新增" :maskClosable="false" :closable="false">
      <div class="modal-body">
        <a-form :model="formState" :rules="rules2" ref="formStateAdd" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }"
          autocomplete="off">
          <a-row>
            <a-col :span="8">
              <a-form-item label="交接影像件id" name="registrationCertificatePhoto">
                <a-input v-model:value="formState.registrationCertificatePhoto" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="存储类型" name="resourceType">
                <a-radio-group v-model:value="formState.resourceType">
                  <a-radio :value="0">nas</a-radio>
                  <a-radio :value="1">资源中心</a-radio>
                </a-radio-group>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="合同号" name="contractNum">
                <a-input v-model:value="formState.contractNum" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="身份证号" name="idCard">
                <a-input v-model:value="formState.idCard" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="业务模式" name="serviceMode">
                <a-select allowClear v-model:value="formState.serviceMode">
                  <a-select-option v-for="(item, index) in typeOptions.options4" :value="item.codeValue" :key="index">
                    {{ item.codeName }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="承租人" name="lesseeName">
                <a-input v-model:value="formState.lesseeName" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="业务类型" name="serviceType">
                <a-select allowClear v-model:value="formState.serviceType">
                  <a-select-option v-for="(item, index) in typeOptions.options3" :value="item.codeValue"
                    :key="item.codeValue">
                    {{ item.codeName }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="档案存储地" name="fileLocation">
                <a-input v-model:value="formState.fileLocation" :disabled="archivesNumDisable" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="车架号" name="carNum">
                <a-input v-model:value="formState.carNum" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="渠道描述" name="channelDesc">
                <a-input v-model:value="formState.channelDesc" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="车牌号" name="licensePlateNum">
                <a-input v-model:value="formState.licensePlateNum" :disabled="archivesNumDisable" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="二网经销商" name="secondDealer">
                <a-input v-model:value="formState.secondDealer" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="档案状态" name="state">
                <a-select allowClear v-model:value="formState.state" :disabled="archivesNumDisable">
                  <a-select-option v-for="(item, index) in typeOptions.options1" :value="item.codeValue"
                    :key="item.codeValue">
                    {{ item.codeName }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="出库来源" name="outboundSource">
                <a-select allowClear v-model:value="formState.outboundSource" :disabled="archivesNumDisable">
                  <a-select-option v-for="(item, index) in typeOptions.options2" :value="item.codeValue"
                    :key="item.codeValue">
                    {{ item.codeName }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <template #footer>
        <div class="modal-btns">
          <div class="modal-btn">
            <a-button type="primary" @click="handleChange({}, 'addOk')">确认</a-button>
          </div>
          <div class="modal-btn">
            <a-button @click="cancel('add')">关闭</a-button>
          </div>
        </div>
      </template>
    </a-modal>
    <div class="shade" v-show="spin">
      <a-spin size="large" />
    </div>
  </div>
</template>
<script setup lang="ts">
import { computed, ref, reactive } from 'vue';
import {
  setDateReturn, updateRows, importExcelUpdateErr, importExcelHomeErr, insertRows
} from "@/api/retailFiles";
import {
  getList, getRows,
  filesOperationRecord,
  downloadTable,
  importExcel
} from "@/api/retailStored";
import { downloadTemplate, getQueryAll } from "@/api/common";
import { getUserInfo } from '@/api/login';
import { downloadFile, formatDate } from "@/utils/index";
import { message } from 'ant-design-vue';
import { MATERIAL } from '@/utils/config';

const spin = ref(false);
const archivesNumDisable = ref(false);
const formStateEdit = ref(null);
const formStateAdd = ref(null);
const queryParams = ref({
  pageNumber: 1,
  pageSize: 10,
  inHomeTime: [formatDate(new Date() - (1000 * 60 * 60 * 24 * 6)), formatDate(new Date())],
  outHomeTime: [],
  warehouseStartTime: "",
  warehouseEndTime: "",
  moveLibStartTime: "",
  moveLibEndTime: "",
});
const uploadErr = reactive({
  tips: "",
  data: [],
  status: 0,
  title: '批量转库'
})
const modalOpen = reactive({
  open1: false,
  open2: false,
  open3: false,
  open4: false,
  open5: false,
  open6: false
});
const typeOptions = reactive({
  options1: [
    {
      codeName: "在库",
      codeType: "state",
      codeValue: 205,
    },
    {
      codeName: "临时出库",
      codeType: "state",
      codeValue: 208,
    },
    {
      codeName: "永久出库",
      codeType: "state",
      codeValue: 209,
    },
    {
      codeName: "待出库",
      codeType: "state",
      codeValue: 206,
    }
  ],
  options2: [],
  options3: [],
  options4: [],
  options5: []
});
const tableData = reactive({
  dataSource: [],
  columns: [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      width: 80,
      align: 'center'
    },
    {
      title: '合同号',
      dataIndex: 'contractNum',
      key: 'contractNum',
    },
    {
      title: '承租人',
      dataIndex: 'lesseeName',
      key: 'lesseeName'
    },
    {
      title: '车架号',
      dataIndex: 'carNum',
      key: 'carNum'
    },
    {
      title: '车牌号',
      dataIndex: 'licensePlateNum',
      key: 'licensePlateNum'
    },
    {
      title: '业务类型',
      dataIndex: 'serviceType',
      key: 'serviceType'
    },
    {
      title: '渠道描述',
      dataIndex: 'channelDesc',
      key: 'channelDesc',
    },
    {
      title: '业务模式',
      dataIndex: 'serviceMode',
      key: 'serviceMode',
    },
    {
      title: '入库时间',
      dataIndex: 'warehouseTime',
      key: 'warehouseTime'
    },
    {
      title: '档案存储地',
      dataIndex: 'fileLocation',
      key: 'fileLocation'
    },
    {
      title: '档案状态',
      dataIndex: 'state',
      key: 'state'
    },
    {
      title: '操作',
      key: 'action',
      width: 200,
      fixed: 'right'
    },
  ],
  modalDataSource: [],
  modalColumns: [
    {
      title: "节点名称",
      dataIndex: "eventNode",
      key: "eventNode",
      width: 120
    },
    {
      title: "操作时间",
      dataIndex: "createdTime",
      key: "createdTime",
      width: 160,
      align: "center",
    },
    {
      title: "操作人",
      dataIndex: "createdBy",
      key: "createdBy",
    },
    {
      title: "备注",
      dataIndex: "remark",
      key: "remark",
    }
  ],
  total: 0,
  loading: false,
});
interface FormState {
  [props: string]: string | number | Array<object> | Array<string>;
}
let formState: FormState = reactive({
});
const isIdCard = (rule: any, value: any) => {
  if (!value) {
    return Promise.reject();
  } else {
    const reg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(([0-2][1-9])|10|20|30|31)\d{3}(\d|X|x)$/;
    const card = reg.test(value);
    if (!card) {
      return Promise.reject('身份证不合法');
    } else {
      return Promise.resolve();
    }
  }
}
const configData = computed(() => {
  let config = {
    itemConfig: [
      {
        type: "input",
        prop: "lesseeName",
        allowClear: "关闭",
        label: "承租人",
        placeholder: "请输入",
      },
      {
        type: "input",
        prop: "contractNum",
        allowClear: "关闭",
        label: "合同号",
        placeholder: "请输入",
      },
      {
        type: "input",
        prop: "carNum",
        allowClear: "关闭",
        label: "车架号",
        placeholder: "请输入",
      },
      {
        type: "input",
        prop: "licensePlateNum",
        allowClear: "关闭",
        label: "车牌号",
        placeholder: "请输入",
      },
      {
        type: "input",
        prop: "idCard",
        allowClear: "关闭",
        label: "身份证号",
        placeholder: "请输入",
      },
      {
        type: "select",
        prop: "state",
        allowClear: "关闭",
        label: "档案状态",
        placeholder: "请输入",
        optList: typeOptions.options1
      },
      {
        type: "select",
        prop: "outboundSource",
        allowClear: "关闭",
        label: "出库来源",
        placeholder: "请输入",
        optList: typeOptions.options2
      },
      {
        type: "dateRange",
        prop: "inHomeTime",
        valueFormat: "YYYY-MM-DD",
        allowClear: "关闭",
        label: "入库时间",
        placeholder: ["请选择", "请选择"]
      },
      {
        type: "dateRange",
        prop: "outHomeTime",
        valueFormat: "YYYY-MM-DD",
        allowClear: "关闭",
        label: "移库时间",
        placeholder: ["请选择", "请选择"]
      },
    ],
    operate: [
      {
        icon: "",
        type: "primary",
        name: "查询",
        handleClick: getLoad,
        rule: true,
      },
      {
        icon: "",
        type: "primary",
        name: "重置",
        handleClick: reset,
        rule: true,
      },
      {
        icon: "",
        type: "primary",
        name: "新增",
        handleClick: insert,
        rule: true,
      },
      {
        icon: "",
        type: "primary",
        name: "批量修改",
        handleClick: batchHome(0),
        rule: true,
      },
      {
        icon: "",
        type: "primary",
        name: "批量转库",
        handleClick: batchHome(1),
        rule: true,
      },
      {
        icon: "",
        type: "primary",
        name: "导出",
        handleClick: exportData,
        rule: true,
      },
      {
        icon: "",
        type: "primary",
        name: "下载模板",
        handleClick: download,
        rule: true,
      },
      {
        icon: "",
        type: "primary",
        name: "设置归还时效",
        handleClick: setTime,
        rule: true,
      },
    ],
    rules: {

    },
  };
  return config;
});
const rules = computed(() => {
  let rules = {
    fileLocation: [
      { required: true, message: "请输入", trigger: "blur" }
    ],
    state: [
      { required: true, message: "请输入", trigger: "blur" }
    ]
  };
  return rules;
});
const rules2 = computed(() => {
  let rules = {
    contractNum: [
      { required: true, message: "请输入", trigger: "blur" }
    ],
    idCard: [
      { required: true, message: "请输入", trigger: "blur" },
      { validator: isIdCard, trigger: "blur" }
    ],
    lesseeName: [
      { required: true, message: "请输入", trigger: "blur" }
    ],
    state: [
      { required: true, message: "请输入", trigger: "blur" }
    ],
    carNum: [
      { required: true, message: "请输入", trigger: "blur" }
    ]
  };
  return rules;
});

function insert() {
  modalOpen.open6 = true;
}
function getLoad() {
  if (queryParams.value.inHomeTime != null && queryParams.value.inHomeTime.length) {
    queryParams.value.warehouseStartTime = queryParams.value.inHomeTime[0] + ' 00:00:00';
    queryParams.value.warehouseEndTime = queryParams.value.inHomeTime[1] + ' 23:59:59';
  } else {
    queryParams.value.warehouseStartTime = "";
    queryParams.value.warehouseEndTime = "";
  }
  if (queryParams.value.outHomeTime != null && queryParams.value.outHomeTime.length) {
    queryParams.value.moveLibStartTime = queryParams.value.outHomeTime[0] + ' 00:00:00';
    queryParams.value.moveLibEndTime = queryParams.value.outHomeTime[1] + ' 23:59:59';
  } else {
    queryParams.value.moveLibStartTime = "";
    queryParams.value.moveLibEndTime = "";
  }
  tableData.loading = true;
  getList({ ...queryParams.value, basicDataVo: { ...queryParams.value }, dataSource: MATERIAL.RETAIL }).then((res: any) => {
    if ((queryParams.value.pageNumber * queryParams.value.pageSize) > res.total && res.total > 0 && queryParams.value.pageNumber > 1) {
      queryParams.value.pageNumber = 1;
      getLoad();
    } else {
      tableData.dataSource = res.records;
      tableData.total = res.total;
      tableData.loading = false;
    }
  });
}
function getOptionsList() {
  getQueryAll({ codeType: 'outboundSource' }).then((res: any) => {
    typeOptions.options2 = res;
  });
  getQueryAll({ codeType: 'serviceType' }).then((res: any) => {
    typeOptions.options3 = res;
  });
  getQueryAll({ codeType: 'serviceMode' }).then((res: any) => {
    typeOptions.options4 = res;
  });
  getQueryAll({ codeType: 'eventType' }).then((res: any) => {
    typeOptions.options5 = res;
  });
}
function reset() {
  queryParams.value = {
    pageNumber: 1,
    pageSize: 10,
    inHomeTime: [],
    outHomeTime: [],
    warehouseStartTime: "",
    warehouseEndTime: "",
    moveLibStartTime: "",
    moveLibEndTime: ""
  }
  getLoad();
}
function exportData() {
  spin.value = true;
  if (queryParams.value.inHomeTime != null && queryParams.value.inHomeTime.length) {
    queryParams.value.warehouseStartTime = queryParams.value.inHomeTime[0] + ' 00:00:00';
    queryParams.value.warehouseEndTime = queryParams.value.inHomeTime[1] + ' 23:59:59';
  } else {
    queryParams.value.warehouseStartTime = "";
    queryParams.value.warehouseEndTime = "";
  }
  if (queryParams.value.outHomeTime != null && queryParams.value.outHomeTime.length) {
    queryParams.value.moveLibStartTime = queryParams.value.outHomeTime[0] + ' 00:00:00';
    queryParams.value.moveLibEndTime = queryParams.value.outHomeTime[1] + ' 23:59:59';
  } else {
    queryParams.value.moveLibStartTime = "";
    queryParams.value.moveLibEndTime = "";
  }
  downloadTable({ ...queryParams.value, dataSource: MATERIAL.RETAIL, excelType: MATERIAL.RETAILFILESEXPORTTYPE, basicDataVo: { ...queryParams.value } }).then((res: any) => {
    if (res) {
      downloadFile(res, "档案管理.xlsx");
    }
    spin.value = false;
  });
}
function download() {
  modalOpen.open5 = true;
}
function currentChange(pageNumber: number, pageSize: number) {
  console.log(pageNumber);
  console.log(pageSize);

  queryParams.value.pageNumber = pageNumber;
  queryParams.value.pageSize = pageSize;
  getLoad();
}
function handleChange(row: any = {}, type: string) {
  switch (type) {
    case "edit":
      getRows({ basicDataVo: { id: row.id, state: row.state }, dataSource: MATERIAL.RETAIL }).then((res: any) => {
        formState = Object.assign(formState, res.materialBasicDataDto);
        modalOpen.open1 = true;
        if (res.materialBasicDataDto['archivesNum']) {
          archivesNumDisable.value = false;
        } else {
          archivesNumDisable.value = true;
        }

      });
      break;
    case "editOk":
      formStateEdit.value.validate().then(() => {
        updateRows({ ...formState, dataSource: MATERIAL.RETAIL }).then((res: any) => {
          if (res) {
            message.success("编辑成功");
            cancel('edit');
            getLoad();
          }
        });
      })
      break;
    case 'record':
      spin.value = true;
      filesOperationRecord({ archivesNum: row.archivesNum }).then(async (res: any) => {
        for (const key in res) {
          res[key]['createdByName'] = await getUserInfo({ createGlobalUserId: res[key]['createdBy'] });
        }
        tableData.modalDataSource = res;
        modalOpen.open2 = true;
        spin.value = false;
      })
      break;
    case 'downloadUpdate':
      spin.value = true;
      downloadTemplate({ type: 'update' }).then((res: any) => {
        downloadFile(res, "批量修改模板.xlsx");
        spin.value = false;
      });
      break;
    case 'downloadDeposit':
      spin.value = true;
      downloadTemplate({ type: 'deposit' }).then((res: any) => {
        downloadFile(res, "批量转库模板.xlsx");
        spin.value = false;
      });
      break;
    case 'downloadErr':
      spin.value = true;
      if (uploadErr.status == 0) {
        importExcelUpdateErr(uploadErr.data).then((res: any) => {
          downloadFile(res, "批量修改失败数据.xlsx");
          spin.value = false;
        })
      } else {
        importExcelHomeErr(uploadErr.data).then((res: any) => {
          downloadFile(res, "批量转库失败数据.xlsx");
          spin.value = false;
        });
      }
      break;
    case 'time':
      if (formState.date) {
        setDateReturn({ date: formState.date }).then((res: any) => {
          if (res) {
            message.success('设置归还时效成功');
          }
          cancel('time');
        });
      }
      break;
    case "addOk":
      formStateAdd.value.validate().then(() => {
        insertRows({ ...formState, dataSource: MATERIAL.RETAIL }).then((res) => {
          if (res) {
            message.success("新增成功");
            cancel('add');
            getLoad();
          }
        });
      })
      break;
    default:
      break;
  }
}
function cancel(type: string) {
  switch (type) {
    case 'edit':
      modalOpen.open1 = false;
      for (const key in formState) {
        delete formState[key];
      }
      break;
    case 'record':
      modalOpen.open2 = false;
      tableData.modalDataSource = [];
      break;
    case 'downloadErr':
      modalOpen.open3 = false;
      uploadErr.tips = "";
      uploadErr.data = [];
      break;
    case 'time':
      formState.date = "";
      modalOpen.open4 = false;
      break;
    case 'downloadTemplate':
      modalOpen.open5 = false;
      break;
    case 'add':
      modalOpen.open6 = false;
      for (const key in formState) {
        delete formState[key];
      }
      formStateAdd.value.clearValidate();
      break;
    default:
      break;
  }
}
function setTime() {
  modalOpen.open4 = true;
}
function batchHome(status: number) {
  return () => {
    let upload = document.createElement("input");
    upload.type = "file";
    upload.addEventListener("change", (e: any) => {
      if (e.target.files.length <= 0) return;
      let file = e.target.files[0];
      let formData = new FormData();
      formData.append("file", file);
      formData.append("dataSource", MATERIAL.RETAIL);
      spin.value = true;
      uploadErr.status = status;

      switch (status) {
        case 0:
          formData.append("importExcelType", MATERIAL.RETAILFILESTYPE);
          importExcel(formData).then((res: any) => {
            if (res) {
              message.success('文件上传成功');
              getLoad();
              for (const key in res) {
                uploadErr.tips = key;
                uploadErr.data = res[key];
              }
              uploadErr.title = '批量修改';
              modalOpen.open3 = true;
            }
            spin.value = false;
          })
          break;
        case 1:
          formData.append("importExcelType", MATERIAL.RETAILHOUSEOUTEXPORTTYPE);
          importExcel(formData).then((res: any) => {
            if (res) {
              message.success('文件上传成功');
              getLoad();
              for (const key in res) {
                uploadErr.tips = key;
                uploadErr.data = res[key];
              }
              uploadErr.title = '批量转库';
              modalOpen.open3 = true;
            }
            spin.value = false;
          })
          break;
        default:
          break;
      }
    });
    upload.click();
  }
}
getLoad();
getOptionsList();
</script>
<style lang="less" scoped></style>
